from django.db.models import Q
from rest_framework import viewsets
from ERP_system.utils.multiple_destory import MultipleDestoryMixin,MultipleOpenMixin
from ERP_system.utils.pagination import GlobalPagination

from basic_info.models import AccountModel
from basic_info.serializer.account_serializer import AccountSerializer

class AccountView(viewsets.ModelViewSet,MultipleDestoryMixin,MultipleOpenMixin):

    queryset = AccountModel.objects.all()

    serializer_class = AccountSerializer


    pagination_class = GlobalPagination

    # def get_serializer_class(self):
    #     if self.action=='list':
    #        return  AccountSerializer
    #
    #     return AccountSerializer
#搜索框过滤查询
    def get_queryset(self):

        if self.action=='list':
            #接受三个可能的参数
            name=self.request.data.get('name',None)
            remark = self.request.data.get('remark', None)
            number_code=self.request.data.get('number_code',None)


            #因为参数可能没有，造成过滤条件是动态的

            query=Q()

            if name:
                query.add(Q(name__contains=name),'AND')
            if remark:
                query.add(Q(remark__contains=remark),'AND')

            if number_code:
                query.add(Q(number_code__contains=number_code),'AND')

            return AccountModel.objects.filter(query).all()

        else:

            return AccountModel.objects.all()
